package br.com.android.financas.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import br.com.android.financas.R;
import br.com.android.financas.model.Categoria;

public class CategoriaSpinnerAdapter extends ArrayAdapter<Categoria> {

	private Activity activity;
	private List<Categoria> categorias=new ArrayList<Categoria>();
	private String prompt;
	public CategoriaSpinnerAdapter(Context context, int textViewResourceId,List<Categoria> objects,String prompt) {
		super(context, textViewResourceId, objects);
		this.activity=(Activity) context;
		this.categorias=objects;

		this.categorias.add(0, new Categoria());
		this.prompt=prompt;

	}

	@Override
	public View getDropDownView(int position, View convertView,
			ViewGroup parent) {
		// TODO Auto-generated method stub
		return getCustomView(position, convertView, parent);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return getCustomView(position, convertView, parent);
	}

	public View getCustomView(int position, View convertView,
			ViewGroup parent) {

		LayoutInflater inflater = activity.getLayoutInflater();
		View row = inflater.inflate(R.layout.cor_item, parent, false);
		TextView cor = (TextView) row.findViewById(R.corItem.cor);
		TextView titulo = (TextView) row.findViewById(R.corItem.titulo);

		Categoria categoria=categorias.get(position);
		if(categoria.getId()==null){

			titulo.setText(prompt);
		}else{
			cor.setBackgroundColor(categoria.getCor());
			titulo.setText(categoria.getNome());
		}
		return row;
	}


}